#!/bin/bash

# SUOTA HarmonyOS 项目最终验证脚本
# 验证所有功能和文档的完整性

echo "🔍 开始 SUOTA HarmonyOS 项目最终验证..."
echo "=================================================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 计数器
TOTAL_CHECKS=0
PASSED_CHECKS=0
FAILED_CHECKS=0

# 检查函数
check_file() {
    TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
    if [ -f "$1" ]; then
        echo -e "${GREEN}✓${NC} $1 存在"
        PASSED_CHECKS=$((PASSED_CHECKS + 1))
        return 0
    else
        echo -e "${RED}✗${NC} $1 缺失"
        FAILED_CHECKS=$((FAILED_CHECKS + 1))
        return 1
    fi
}

check_directory() {
    TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
    if [ -d "$1" ]; then
        echo -e "${GREEN}✓${NC} 目录 $1 存在"
        PASSED_CHECKS=$((PASSED_CHECKS + 1))
        return 0
    else
        echo -e "${RED}✗${NC} 目录 $1 缺失"
        FAILED_CHECKS=$((FAILED_CHECKS + 1))
        return 1
    fi
}

check_content() {
    TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
    if [ -f "$1" ] && grep -q "$2" "$1"; then
        echo -e "${GREEN}✓${NC} $1 包含 '$2'"
        PASSED_CHECKS=$((PASSED_CHECKS + 1))
        return 0
    else
        echo -e "${RED}✗${NC} $1 不包含 '$2'"
        FAILED_CHECKS=$((FAILED_CHECKS + 1))
        return 1
    fi
}

echo -e "\n${BLUE}1. 核心组件文件检查${NC}"
echo "----------------------------------------"

# 核心插件文件
check_file "components/plugin/AutoSuota.ets"
check_file "components/plugin/SuotaProtocol.ets"
check_file "components/plugin/SuotaScanner.ets"
check_file "components/plugin/SuotaBluetoothManager.ets"
check_file "components/plugin/SuotaLibConfig.ets"
check_file "components/plugin/SuotaUtils.ets"
check_file "components/plugin/SpeedStatistics.ets"

# 头部信息处理
check_file "components/plugin/HeaderInfo.ets"

echo -e "\n${BLUE}2. 页面和测试文件检查${NC}"
echo "----------------------------------------"

# 页面文件
check_file "pages/SuotaTestPage.ets"
check_file "pages/TestEntryAbility.ets"

# 测试文件
check_file "tests/SuotaCoreTest.ets"

echo -e "\n${BLUE}3. 配置文件检查${NC}"
echo "----------------------------------------"

# 配置文件
check_file "module.json5"
check_file "oh-package.json5"
check_file "build-profile.json5"

echo -e "\n${BLUE}4. 文档文件检查${NC}"
echo "----------------------------------------"

# 文档文件
check_file "README.md"
check_file "EXAMPLE.md"

echo -e "\n${BLUE}5. 权限配置检查${NC}"
echo "----------------------------------------"

# 检查蓝牙权限
check_content "module.json5" "ohos.permission.ACCESS_BLUETOOTH"
check_content "module.json5" "ohos.permission.DISCOVER_BLUETOOTH"
check_content "module.json5" "ohos.permission.MANAGE_BLUETOOTH"

echo -e "\n${BLUE}6. 依赖配置检查${NC}"
echo "----------------------------------------"

# 检查依赖
if [ -f "oh-package.json5" ]; then
    check_content "oh-package.json5" "dependencies"
fi

echo -e "\n${BLUE}7. 代码质量检查${NC}"
echo "----------------------------------------"

# 统计代码行数
if command -v find >/dev/null 2>&1; then
    TOTAL_LINES=$(find . -name "*.ets" -exec wc -l {} + 2>/dev/null | tail -1 | awk '{print $1}' || echo "0")
    echo -e "${GREEN}✓${NC} 总代码行数: $TOTAL_LINES"
    PASSED_CHECKS=$((PASSED_CHECKS + 1))
else
    echo -e "${YELLOW}⚠${NC} 无法统计代码行数"
fi
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))

# 检查核心类的导出
echo -e "\n${BLUE}8. 核心功能验证${NC}"
echo "----------------------------------------"

# 检查 AutoSuota 类
check_content "components/plugin/AutoSuota.ets" "export class AutoSuota"
check_content "components/plugin/AutoSuota.ets" "startUpgrade"
check_content "components/plugin/AutoSuota.ets" "setDelegate"

# 检查 SuotaProtocol 类
check_content "components/plugin/SuotaProtocol.ets" "export class SuotaProtocol"
check_content "components/plugin/SuotaProtocol.ets" "uploadFirmware"

# 检查 SuotaScanner 类
check_content "components/plugin/SuotaScanner.ets" "export class SuotaScanner"
check_content "components/plugin/SuotaScanner.ets" "startScan"

# 检查配置管理
check_content "components/plugin/SuotaLibConfig.ets" "export class SuotaLibConfig"
check_content "components/plugin/SuotaLibConfig.ets" "setConfig"

echo -e "\n${BLUE}9. 测试功能验证${NC}"
echo "----------------------------------------"

# 检查测试页面
check_content "pages/SuotaTestPage.ets" "SuotaTestPage"
check_content "pages/SuotaTestPage.ets" "runCoreTests"

# 检查核心测试
check_content "tests/SuotaCoreTest.ets" "SuotaCoreTest"
check_content "tests/SuotaCoreTest.ets" "runAllTests"

echo -e "\n${BLUE}10. 文档完整性检查${NC}"
echo "----------------------------------------"

# 检查 README 内容
check_content "README.md" "SUOTA"
check_content "README.md" "快速开始"
check_content "README.md" "API 参考"
check_content "README.md" "测试指南"

# 检查示例文档
check_content "EXAMPLE.md" "使用示例"
check_content "EXAMPLE.md" "SimpleSuotaExample"

echo -e "\n${BLUE}11. 项目结构验证${NC}"
echo "----------------------------------------"

# 检查目录结构
check_directory "components"
check_directory "components/plugin"
check_directory "pages"
check_directory "tests"

echo -e "\n${BLUE}12. 构建配置验证${NC}"
echo "----------------------------------------"

# 检查构建配置
if [ -f "build-profile.json5" ]; then
    check_content "build-profile.json5" "apiType"
    check_content "build-profile.json5" "buildOption"
fi

echo -e "\n=================================================="
echo -e "${BLUE}验证结果汇总${NC}"
echo "=================================================="

echo -e "总检查项: ${BLUE}$TOTAL_CHECKS${NC}"
echo -e "通过检查: ${GREEN}$PASSED_CHECKS${NC}"
echo -e "失败检查: ${RED}$FAILED_CHECKS${NC}"

# 计算通过率
if [ $TOTAL_CHECKS -gt 0 ]; then
    PASS_RATE=$((PASSED_CHECKS * 100 / TOTAL_CHECKS))
    echo -e "通过率: ${BLUE}$PASS_RATE%${NC}"
    
    if [ $PASS_RATE -ge 95 ]; then
        echo -e "\n${GREEN}🎉 项目验证通过！所有核心功能和文档都已完整。${NC}"
        echo -e "${GREEN}✅ 项目已准备好进行构建和测试。${NC}"
    elif [ $PASS_RATE -ge 80 ]; then
        echo -e "\n${YELLOW}⚠️  项目基本完整，但有一些小问题需要修复。${NC}"
    else
        echo -e "\n${RED}❌ 项目存在重要问题，需要进一步完善。${NC}"
    fi
else
    echo -e "\n${RED}❌ 无法进行验证。${NC}"
fi

echo -e "\n${BLUE}建议的下一步操作:${NC}"
echo "1. 🔧 运行 HarmonyOS DevEco Studio 构建项目"
echo "2. 🧪 在设备或模拟器上测试核心功能"
echo "3. 📱 测试 BLE 设备扫描和连接"
echo "4. 📦 测试固件文件解析和升级流程"
echo "5. 📊 验证进度监控和错误处理"
echo "6. 📚 根据测试结果更新文档"

echo -e "\n${BLUE}性能优化建议:${NC}"
echo "• 🚀 优化 BLE 连接和数据传输"
echo "• 💾 实现配置持久化存储"
echo "• 🔄 添加断点续传功能"
echo "• 🛡️  增强错误恢复机制"
echo "• 📈 优化内存使用和性能"

echo -e "\n${GREEN}验证完成！${NC}"